package br.roove.bomotivo.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import br.roove.bomotivo.model.Item;
import br.roove.bomotivo.util.JSONParser;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class ListEventsFotos extends ListFragment{
	
	SharedPreferences.Editor prefs_editor;
	SharedPreferences prefs;
	String MY_PREFS = "eventospref";
	private ListView listview;
	private CustomArrayAdapterPhotosEvent mAdapter;
	private ArrayList<Item> itens;
	private JSONParser jsonParser;
	private String URL = "http://bomotivo.roove.com.br/service/eventos/galerias.json";
	String BASE_URL = "http://bomotivo.roove.com.br";
	ProgressDialog pd;
	String evento = "";
	ArrayList<String> imagesToAdapter;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		prefs_editor = this.getActivity().getSharedPreferences(MY_PREFS, 0).edit();
		prefs = this.getActivity().getSharedPreferences(MY_PREFS, 0);
		

		View rootView = inflater.inflate(R.layout.list_events_fotos, container, false);
		listview = (ListView)rootView.findViewById(android.R.id.list);
		jsonParser  = new JSONParser(getActivity());
		return rootView;
	}
	
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		imagesToAdapter = new ArrayList<String>();
		//String json = jsonParser.getJSONString(URL);
		AsyncHttpClient client = new AsyncHttpClient();
		client.get(URL, null, new JsonHttpResponseHandler(){
			
			
			

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				pd = ProgressDialog.show(getActivity(), null, "Buscando...");
			}

			@Override
			public void onSuccess(JSONArray arg0) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0);
				pd.dismiss();
				
				itens = new ArrayList<Item>();
			
		JSONArray jsonArray = new JSONArray();
		try {
			jsonArray = arg0;
			for (int i = 0; i < jsonArray.length(); i++){
				Item item = new Item();
				JSONObject jsonObject = new JSONObject();
				jsonObject = jsonArray.getJSONObject(i);
				item.setImagen(jsonObject.getInt("imagens"));
				evento = jsonObject.getString("nome");
				item.setId(jsonObject.getInt("id"));
				item.setData(jsonObject.getString("data"));
				item.setEvento(jsonObject.getString("nome"));
				item.setInfo(jsonObject.getString("info"));
				item.setNumero_de_pessoas(jsonObject.getInt("participantes"));
				item.setValor(jsonObject.getInt("valor"));
				if (jsonObject.getString("thumb_reduzido").equals("")){
					item.setURL_foto_evento(BASE_URL + jsonObject.getString("thumb_original"));
					item.setURL_foto_lista(BASE_URL + jsonObject.getString("thumb_original"));
				}else{
					item.setURL_foto_lista(BASE_URL + jsonObject.getString("thumb_reduzido"));
				}
				item.setQtd_fotos(jsonObject.getString("imagens"));
				itens.add(item);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		mAdapter = new CustomArrayAdapterPhotosEvent(getActivity(), android.R.id.list, itens);
		listview.setAdapter(mAdapter);
	}
	});
	}

		
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		final int index = position;
		
		final ArrayList<String> imagesToLoad = new ArrayList<String>();
		
		
		AsyncHttpClient client = new AsyncHttpClient();
		client.get("http://bomotivo.roove.com.br/service/imagens/" + itens.get(position).getId() + ".json", null, new JsonHttpResponseHandler(){
			
			
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				pd = ProgressDialog.show(getActivity(), null, "Buscando fotos...");
			}

			JSONArray jsonArray = new JSONArray();
			
			@Override
			public void onSuccess(JSONArray arg0) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0);
				
				pd.dismiss();
				
				jsonArray = arg0;
				for (int i = 0; i<jsonArray.length();i++){
					String imageToLoad = "";
					JSONObject jsonObject = new JSONObject();
					try {
						jsonObject = jsonArray.getJSONObject(i);
						imageToLoad = "http://bomotivo.roove.com.br" + jsonObject.getString("original");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					imagesToLoad.add(imageToLoad);
					
				}
				
				Bundle bundle = new Bundle();
				bundle.putString("nome", itens.get(index).getEvento());
				bundle.putString("data", itens.get(index).getData());
				bundle.putInt("id", itens.get(index).getId());
				
				imagesToAdapter = imagesToLoad;
				
				//((MainActivity)getActivity()).changeTabText(evento);
				Galeria galeria = new Galeria(imagesToAdapter, evento);
				galeria.setArguments(bundle);
				getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_main, galeria).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).addToBackStack("galeria").commit();
			}
			
			
		});
		
		
	}

}
